package com.letv.whatslive.model.convert;

import com.google.common.collect.Lists;
import com.letv.whatslive.common.utils.Constants;
import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.Program;
import com.letv.whatslive.model.utils.Constant;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBRef;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by gaoshan on 15-7-13.
 */
public class ProgramConvert {


    public static DBObject castProgramToDBObject(Program program) {
        DBObject dbo = new BasicDBObject();
        if (program.getId() != null) {
            dbo.put("_id", program.getId());
        }
        if (program.getDeleteStatus() != null)
            dbo.put("deleteStatus", ObjUtils.toInteger(program.getDeleteStatus()));
        dbo.put("pName", ObjUtils.ifNull(program.getPName(), ""));
        dbo.put("pDesc", ObjUtils.ifNull(program.getPDesc(), ""));
        dbo.put("pType", ObjUtils.toInteger(program.getPType()));
        dbo.put("activityId", ObjUtils.toString(program.getActivityId(), ""));
        dbo.put("videoId", ObjUtils.toString(program.getVideoId(), ""));
        dbo.put("userId", ObjUtils.toLong(program.getUserId()));
        dbo.put("status", ObjUtils.ifNull(program.getStatus(), 1));
        dbo.put("picture", ObjUtils.ifNull(program.getPicture(), Constant.DEFAULT_PICTURE));
        dbo.put("bigPicture", ObjUtils.ifNull(program.getBigPicture(), ""));
        dbo.put("listPicture", ObjUtils.ifNull(program.getListPicture(), ""));
        dbo.put("gifPicture", ObjUtils.ifNull(program.getGifPicture(), ""));

        dbo.put("likeNum", ObjUtils.toLong(program.getLikeNum(), 0L));
        dbo.put("watchNum", ObjUtils.toLong(program.getWatchNum(), 0L));
        dbo.put("watchCount", ObjUtils.toLong(program.getWatchCount(), 0L));
        dbo.put("commentNum", ObjUtils.toLong(program.getCommentNum(), 0L));
        dbo.put("realLikeNum", program.getRealLikeNum());
        dbo.put("realWatchNum", program.getRealWatchNum());
        dbo.put("realCommentNum", program.getRealCommentNum());
        dbo.put("realWatchCount", program.getRealWatchCount());
        dbo.put("startTime", ObjUtils.toLong(program.getStartTime(), System.currentTimeMillis()));
        dbo.put("endTime", ObjUtils.toLong(program.getEndTime(), System.currentTimeMillis()));
        dbo.put("createTime", ObjUtils.toLong(program.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", ObjUtils.toLong(program.getUpdateTime(), System.currentTimeMillis()));
        dbo.put("priority", ObjUtils.toLong(program.getPriority(), 0L));
        dbo.put("isPrivacy", ObjUtils.toInteger(program.getIsPrivacy(), 0));
        dbo.put("isShow", ObjUtils.toInteger(program.getIsShow(), 1));
        dbo.put("from", ObjUtils.toInteger(program.getFrom(), 1));
        dbo.put("location", ObjUtils.toString(program.getLocation(), ""));
        dbo.put("vuid", ObjUtils.toString(program.getVuid(), ""));
        dbo.put("province", ObjUtils.toString(program.getProvince()));
        dbo.put("city", ObjUtils.toString(program.getCity()));
        dbo.put("primaryPName", ObjUtils.toString(program.getPrimaryPName(), program.getPName()));
//        if(program.getLiveId()!=null && program.getLiveId().length()>0){
//            dbo.put("liveId", ObjUtils.toString(program.getLiveId(),""));
//        }
        dbo.put("liveId", ObjUtils.toString(program.getLiveId(), ""));
        if (program.getIsCarousel() != null) {
            dbo.put("isCarousel", program.getIsCarousel());
        }
        if (program.getIsContinue() != null) {
            dbo.put("isContinue", program.getIsContinue());
        }
        if (program.getOrderNum() != null) {
            dbo.put("orderNum", program.getOrderNum());
        }
        if (program.getDisplayOrderNum() != null) {
            dbo.put("displayOrderNum", program.getDisplayOrderNum());
        }
        if (program.getTagList() != null && program.getTagList().size() > 0) {
            dbo.put("tagList", program.getTagList());
        } else {
            dbo.put("tagList", new ArrayList<Long>());
        }
        if (program.getAssorts() != null) {
            dbo.put("assorts", program.getAssorts());
        }
        if (program.getActivity() != null) {
            dbo.put("activity", program.getActivity());
        }
        dbo.put("playTime", ObjUtils.toLong(program.getPlayTime(), 0L));
        dbo.put("isPrivacy", ObjUtils.toInteger(program.getIsPrivacy(), 0));
        dbo.put("requestHeader", ObjUtils.toString(program.getRequestHeader(), ""));
        dbo.put("source", ObjUtils.toString(program.getSource(), ""));
        dbo.put("param1", ObjUtils.toString(program.getParam1(), ""));
        dbo.put("param2", ObjUtils.toString(program.getParam2(), ""));
        dbo.put("param3", ObjUtils.toString(program.getParam3(), ""));
        dbo.put("machineCount", ObjUtils.toInteger(program.getMachineCount(), 1));
        dbo.put("replayConvertStatus", ObjUtils.toString(program.getReplayConvertStatus(), ""));
        dbo.put("liveServiceProvider", ObjUtils.toInteger(program.getLiveServiceProvider(), Constants.LIVE_SERVICE_PROVIDER_LE));
        dbo.put("tencentChannelId", ObjUtils.toString(program.getTencentChannelId(), ""));
        dbo.put("activityInfo", ObjUtils.toString(program.getActivityInfo(), ""));
        dbo.put("videoInfoAndroid", ObjUtils.toString(program.getVideoInfoAndroid(), ""));
        dbo.put("videoInfoIOS", ObjUtils.toString(program.getVideoInfoIOS(), ""));
        dbo.put("videoInfoH5", ObjUtils.toString(program.getVideoInfoH5(), ""));
        dbo.put("videoInfoPC", ObjUtils.toString(program.getVideoInfoPC(), ""));
        if (program.getSourceType() != null) {
            dbo.put("sourceType", ObjUtils.toInteger(program.getSourceType()));
        }
        dbo.put("appId", ObjUtils.toString(program.getAppId(), "10000"));

        dbo.put("uploadPic", ObjUtils.toInteger(program.getUploadPic()));

//        if(null != program.getIndexOrder()){
//            dbo.put("indexOrder", ObjUtils.toInteger(program.getIndexOrder()));
//        }

        dbo.put("mid", ObjUtils.toInteger(program.getMid()));
        dbo.put("vid", ObjUtils.toInteger(program.getVid()));
        dbo.put("videoUploadStatus", ObjUtils.toInteger(program.getVideoUploadStatus()));
        dbo.put("videoUploadRole", ObjUtils.toInteger(program.getVideoUploadRole()));
        dbo.put("videoUploadReviewStatus",
                ObjUtils.toInteger(program.getVideoUploadReviewStatus(), Constants.VIDEO_UPLOAD_FIRST_REVIEW_STATUS_IN_REVIEW));
        dbo.put("userRole", ObjUtils.toInteger(program.getUserRole(), 0));
        dbo.put("backupProgramId", ObjUtils.toString(program.getBackupProgramId(), ""));
        if (program.getLocationToShow() != null && program.getLocationToShow().size() > 0) {
            dbo.put("locationToShow", program.getLocationToShow());
        } else {
            dbo.put("locationToShow", new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
        }
        if (null != program.getCustomCommentId()) {
            dbo.put("customCommentId", ObjUtils.toLong(program.getCustomCommentId()));
        }
        if (null != program.getIsLandscape()) {
            dbo.put("isLandscape", ObjUtils.toInteger(program.getIsLandscape()));
        }
        return dbo;
    }

    public static Program castDBObjectToProgram(DBObject dbObject) {
        Program program = new Program();
        if (dbObject != null) {
            program.setId(ObjUtils.toLong(dbObject.get("_id")));
            program.setPName(ObjUtils.toString(dbObject.get("pName")));
            program.setPDesc(ObjUtils.toString(dbObject.get("pDesc")));
            program.setPType(ObjUtils.toInteger(dbObject.get("pType")));
            program.setUserId(ObjUtils.toLong(dbObject.get("userId")));
            program.setUserRef((DBRef) dbObject.get("user"));
            program.setStatus(ObjUtils.toInteger(dbObject.get("status")));
            program.setPicture(ObjUtils.toString(dbObject.get("picture"), Constant.DEFAULT_PICTURE));
            program.setBigPicture(ObjUtils.toString(dbObject.get("bigPicture"), ""));
            program.setListPicture(ObjUtils.toString(dbObject.get("listPicture"), ""));
            program.setGifPicture(ObjUtils.toString(dbObject.get("gifPicture"), ""));
            program.setCommentNum(ObjUtils.toLong(dbObject.get("commentNum"), 0L));
            program.setLikeNum(ObjUtils.toLong(dbObject.get("likeNum"), 0L));
            program.setWatchNum(ObjUtils.toLong(dbObject.get("watchNum"), 0L));
            program.setWatchCount(ObjUtils.toLong(dbObject.get("watchCount"), 0L));

            program.setRealCommentNum(ObjUtils.toLong(dbObject.get("realCommentNum"), 0L));
            program.setRealLikeNum(ObjUtils.toLong(dbObject.get("realLikeNum"), 0L));
            program.setRealWatchNum(ObjUtils.toLong(dbObject.get("realWatchNum"), 0L));
            program.setRealWatchCount(ObjUtils.toLong(dbObject.get("realWatchCount"), 0L));

//            program.setReportNum(ObjUtils.toLong(dbObject.get("reportNum"), 0L));
            program.setStartTime(ObjUtils.toLong(dbObject.get("startTime")));
            program.setEndTime(ObjUtils.toLong(dbObject.get("endTime")));
            program.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            program.setUpdateTime(ObjUtils.toLong(dbObject.get("updateTime")));
            program.setPriority(ObjUtils.toLong(dbObject.get("priority"), 0L));
            program.setIsShow(ObjUtils.toInteger(dbObject.get("isShow"), 1));
            program.setOrderNum(ObjUtils.toLong(dbObject.get("orderNum"), 0L));
            program.setDisplayOrderNum(ObjUtils.toLong(dbObject.get("displayOrderNum"), 0L));
            program.setFrom(ObjUtils.toInteger(dbObject.get("from"), 1));
            program.setLiveId(ObjUtils.toString(dbObject.get("liveId")));
            program.setIsCarousel(ObjUtils.toInteger(dbObject.get("isCarousel")));
            program.setActivityId(ObjUtils.toString(dbObject.get("activityId")));
            program.setVideoId(ObjUtils.toString(dbObject.get("videoId")));
            program.setVuid(ObjUtils.toString(dbObject.get("vuid"), ""));
            program.setLocation(ObjUtils.toString(dbObject.get("location"), ""));
            program.setPrimaryPName(ObjUtils.toString(dbObject.get("primaryPName"), program.getPName()));
            program.setIsContinue(ObjUtils.toInteger(dbObject.get("isContinue")));
            program.setProvince(ObjUtils.toString(dbObject.get("province")));
            program.setCity(ObjUtils.toString(dbObject.get("city")));
            program.setDeleteStatus(ObjUtils.toInteger(dbObject.get("deleteStatus")));
            if (dbObject.get("tagList") != null) {
                BasicDBList dbList = (BasicDBList) (dbObject.get("tagList"));
                if (dbList.size() > 0) {
                    List<Long> tagList = new ArrayList<Long>();
                    for (int i = 0; i < dbList.size(); i++) {
                        tagList.add(ObjUtils.toLong(dbList.get(i)));
                    }
                    program.setTagList(tagList);
                }
            }
            if (dbObject.get("assorts") != null) {
                if (dbObject.get("assorts") instanceof BasicDBList) {
                    BasicDBList dbList = (BasicDBList) (dbObject.get("assorts"));
                    if (dbList.size() > 0) {
                        List<Map<String, Object>> assorts = Lists.newArrayList();
                        for (int i = 0; i < dbList.size(); i++) {
                            if (dbList.get(i) instanceof Map) {
                                assorts.add((Map<String, Object>) (dbList.get(i)));
                            } else {
                                throw new ClassCastException("assorts can not cast to Map!");
                            }
                        }
                        program.setAssorts(assorts);
                    }
                } else if (dbObject.get("assorts") instanceof BasicDBObject && ((BasicDBObject) dbObject.get("assorts")).size() > 0) {
                    List<Map<String, Object>> assorts = Lists.newArrayList();
                    Map<String, Object> ass = ((BasicDBObject) (dbObject.get("assorts"))).toMap();
                    assorts.add(ass);
                    program.setAssorts(assorts);
                }

            }

            if (dbObject.get("activity") != null) {
                if (dbObject.get("activity") instanceof BasicDBList) {
                    BasicDBList dbList = (BasicDBList) (dbObject.get("activity"));
                    if (dbList.size() > 0) {
                        List<Map<String, Object>> activity = Lists.newArrayList();
                        for (int i = 0; i < dbList.size(); i++) {
                            if (dbList.get(i) instanceof Map) {
                                activity.add((Map<String, Object>) (dbList.get(i)));
                            } else {
                                throw new ClassCastException("activity can not cast to Map!");
                            }
                        }
                        program.setActivity(activity);
                    }
                } else if (dbObject.get("activity") instanceof BasicDBObject && ((BasicDBObject) dbObject.get("activity")).size() > 0) {
                    List<Map<String, Object>> activity = Lists.newArrayList();
                    Map<String, Object> act = ((BasicDBObject) (dbObject.get("activity"))).toMap();
                    activity.add(act);
                    program.setActivity(activity);
                }
            }

            program.setPlayTime(ObjUtils.toLong(dbObject.get("playTime"), 0L));
            program.setRequestHeader(ObjUtils.toString(dbObject.get("requestHeader"), ""));
            program.setIsPrivacy(ObjectUtils.toInteger(dbObject.get("isPrivacy"), 0));
            program.setSource(ObjectUtils.toString(dbObject.get("source"), ""));
            program.setParam1(ObjectUtils.toString(dbObject.get("param1"), ""));
            program.setParam2(ObjectUtils.toString(dbObject.get("param2"), ""));
            program.setParam3(ObjectUtils.toString(dbObject.get("param3"), ""));
            program.setMachineCount(ObjectUtils.toInteger(dbObject.get("machineCount"), 1));
            program.setReplayConvertStatus(ObjectUtils.toString(dbObject.get("replayConvertStatus"), ""));
            program.setLiveServiceProvider(ObjectUtils.toInteger(dbObject.get("liveServiceProvider"), Constants.LIVE_SERVICE_PROVIDER_LE));
            program.setTencentChannelId(ObjectUtils.toString(dbObject.get("tencentChannelId"), ""));
            program.setActivityInfo(ObjectUtils.toString(dbObject.get("activityInfo"), ""));
            program.setVideoInfoAndroid(ObjectUtils.toString(dbObject.get("videoInfoAndroid"), ""));
            program.setVideoInfoIOS(ObjectUtils.toString(dbObject.get("videoInfoIOS"), ""));
            program.setVideoInfoH5(ObjectUtils.toString(dbObject.get("videoInfoH5"), ""));
            program.setVideoInfoPC(ObjectUtils.toString(dbObject.get("videoInfoPC"), ""));
            program.setSourceType(ObjectUtils.toInteger(dbObject.get("sourceType"), 0));
            program.setAppId(ObjectUtils.toString(dbObject.get("appId"), "10000"));
            program.setUploadPic(ObjectUtils.toInteger(dbObject.get("uploadPic")));
//            program.setIndexOrder(ObjectUtils.toInteger(dbObject.get("indexOrder"), 1));
            program.setMid(ObjectUtils.toLong(dbObject.get("mid")));
            program.setVid(ObjectUtils.toLong(dbObject.get("vid")));
            program.setVideoUploadStatus(ObjectUtils.toInteger(dbObject.get("videoUploadStatus")));
            program.setVideoUploadRole(ObjectUtils.toInteger(dbObject.get("videoUploadRole")));
            program.setVideoUploadReviewStatus(
                    ObjectUtils.toInteger(dbObject.get("videoUploadReviewStatus"), Constants.VIDEO_UPLOAD_FIRST_REVIEW_STATUS_IN_REVIEW));
            program.setUserRole(ObjectUtils.toInteger(dbObject.get("userRole"), 0));
            if (dbObject.get("backupProgramId") != null)
                program.setBackupProgramId(ObjectUtils.toString(dbObject.get("backupProgramId")));
            if (dbObject.get("locationToShow") != null) {
                BasicDBList dbList = (BasicDBList) (dbObject.get("locationToShow"));
                if (dbList.size() > 0) {
                    List<Integer> locationToShow = new ArrayList<Integer>();
                    for (int i = 0; i < dbList.size(); i++) {
                        locationToShow.add(ObjUtils.toInteger(dbList.get(i)));
                    }
                    program.setLocationToShow(locationToShow);
                }
            } else {
                program.setLocationToShow(new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
            }

            program.setUserRole(ObjectUtils.toInteger(dbObject.get("userRole"), 0));

            program.setCustomCommentId(ObjectUtils.toLong(dbObject.get("customCommentId")));

            program.setIsLandscape(ObjUtils.toInteger(dbObject.get("isLandscape")));

        } else {
            return null;
        }
        return program;
    }
}
